<?php


namespace api\app\controller;

use api\app\model\UserModel;
use cmf\controller\RestBaseController;
use think\facade\Db;
use think\Request;

class MoneyController extends RestBaseController
{
    //充值余额
    public function add(Request $request)
    {
        $id = $request->token;
        //接收
        $money = input('post.money','');
        //判断
        if (empty($money)) {
            $this->error('参数错误');
        }
        $user = UserModel::find($id);
        if (!$user) {
            $this->error('错误');
        }
        //支付验证
        //支付验证


        // 启动事务
        Db::startTrans();

        try {
            //记录
            $addLog = addMoneyLog($id,$money,'充值余额',$user['money'],1);
            //充值
            $inc = UserModel::where('id',$id)->inc('money',$money)->update();

        } catch (\Exception $exception) {
            // 回滚事务
            Db::rollback();
            $this->error('充值失败');
        }

        if ($inc && $addLog) {
            // 提交事务
            Db::commit();
            $this->success('充值成功');
        }else{
            // 回滚事务
            Db::rollback();
            $this->error('充值失败');
        };

    }

}